import { RouteRecordRaw, createRouter, createWebHashHistory } from 'vue-router';
const homepageModules = import.meta.glob("./modules/**/homepage.ts", { eager: true });

const fixedModules = import.meta.glob("./modules/**/!(homepage).ts", { eager: true });

const defaultRouterList: Array<any> = [
  {
    path: '/',
    redirect: '/film/index',
    component: () => import('../layouts/blank.vue'),
  },
  {
    path: '/:w+',
    name: '404Page',
    redirect: '/result/404',
  },
];

export const homepageRouterList: Array<RouteRecordRaw> = mapModuleRouterList(homepageModules);
export const fixedRouterList: Array<RouteRecordRaw> = mapModuleRouterList(fixedModules);

export const allRoutes = [...homepageRouterList, ...fixedRouterList, ...defaultRouterList];

export function mapModuleRouterList(modules: Record<string, any>): Array<RouteRecordRaw> {
  const routerList: Array<RouteRecordRaw> = [];
  Object.keys(modules).forEach((key) => {
    const mod: any = modules[key].default || {};
    const modList = Array.isArray(mod) ? [...mod] : [mod];
    routerList.push(...modList);
  });
  return routerList;
}



const router = createRouter({
  history: createWebHashHistory(),
  routes: allRoutes,
  scrollBehavior() {
    return {
      el: '#app',
      top: 0,
      behavior: 'smooth',
    }
  },
});

export default router;
